﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CApCMContinuousMacroManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension.ModelObjects;
using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal class CApCMContinuousMacroManager
{
  private TxComponent displayParent;
  private CApCMContinuousMacroDisplayer macroDisplayer;
  private CApCMContinuousMacroProjectionDisplayer projectionDisplayer;
  private ITxLocatableObject part;
  private ProjectionWarning projectionWarning;
  private bool isPreviewVisible;
  private bool isPreviewCutThrough;

  public bool IsPreviewVisible
  {
    get => this.isPreviewVisible;
    set
    {
      if (this.isPreviewVisible == value)
        return;
      this.isPreviewVisible = value;
      this.SetProjectionDisplayer();
    }
  }

  public bool IsPreviewCutThrough
  {
    get => this.isPreviewCutThrough;
    set
    {
      if (this.isPreviewCutThrough == value)
        return;
      this.isPreviewCutThrough = value;
      CApCMContinuousMacroProjectionDisplayer.IsProjectionCutThrough = value;
      this.SetProjectionDisplayer();
    }
  }

  public bool IsProjectionFlippped
  {
    get => CApCMContinuousMacroProjectionDisplayer.IsProjectionFlippped;
    set
    {
      CApCMContinuousMacroProjectionDisplayer.IsProjectionFlippped = !CApCMContinuousMacroProjectionDisplayer.IsProjectionFlippped;
      this.SetProjectionDisplayer();
    }
  }

  public TxRoboticViaLocationOperation ViaLocation => this.macroDisplayer.ViaLocation;

  public CApCMShape Shape
  {
    get
    {
      return this.macroDisplayer.CreationData != null ? this.macroDisplayer.CreationData.Shape : CApCMShape.None;
    }
  }

  public ITxLocatableObject Part
  {
    get => this.part;
    set
    {
      if (this.part == value)
        return;
      this.part = value;
      if (this.macroDisplayer.CreationData != null)
        this.macroDisplayer.CreationData.Part = this.part;
      CApCMContinuousMacroProjectionDisplayer.IsProjectionFlippped = false;
      this.SetProjectionDisplayer();
    }
  }

  public ProjectionWarning ProjectionWarning
  {
    get => this.projectionWarning;
    private set
    {
      if (this.projectionWarning == value)
        return;
      this.projectionWarning = value;
      this.FireSendFeedback();
    }
  }

  public event Action SendFeedback;

  public CApCMContinuousMacroManager(
    TxRoboticViaLocationOperation viaLocation,
    CApCMMacroShape shape)
  {
    CApCMContinuousMacroProjectionDisplayer.IsProjectionFlippped = false;
    CApCMContinuousMacroProjectionDisplayer.IsProjectionCutThrough = true;
    this.isPreviewCutThrough = true;
    this.displayParent = CApCMContinuousMacroUtilities.CreateDisplayParent();
    this.macroDisplayer = new CApCMContinuousMacroDisplayer(viaLocation, this.displayParent, shape);
    this.Part = shape.Part;
  }

  public void Delete()
  {
    if (this.projectionDisplayer != null)
    {
      this.projectionDisplayer.Delete();
      this.projectionDisplayer = (CApCMContinuousMacroProjectionDisplayer) null;
    }
    if (this.macroDisplayer != null)
    {
      this.macroDisplayer.Delete();
      this.macroDisplayer = (CApCMContinuousMacroDisplayer) null;
    }
    if (TxComponent.op_Inequality(this.displayParent, (ITxObject) null))
    {
      ((TxBaseGeometryCreationObject) this.displayParent).Delete();
      this.displayParent = (TxComponent) null;
    }
    TxApplication.RefreshDisplay();
  }

  public void SetMacro(CApCMShape shapeType, params double[] macroParameters)
  {
    if (shapeType != CApCMShape.None)
      this.macroDisplayer.CreationData = CApCMMacroShape.Create(shapeType, this.Part, macroParameters);
    else
      this.macroDisplayer.CreationData = (CApCMMacroShape) null;
    this.SetProjectionDisplayerData();
  }

  public void SetMacroParameters(params double[] macroParameters)
  {
    this.macroDisplayer.CreationData = CApCMMacroShape.Create(this.macroDisplayer.CreationData.Shape, this.Part, macroParameters);
    this.SetProjectionDisplayerData();
  }

  public void Save()
  {
    TxApplication.ActiveUndoManager.StartTransaction();
    if (this.macroDisplayer.CreationData != null)
      this.macroDisplayer.CreationData.Store(this.macroDisplayer.ViaLocation);
    else
      CApCMMacroShape.RemoveAllMacroShapeParameters(this.macroDisplayer.ViaLocation);
    TxApplication.ActiveUndoManager.EndTransaction();
    new TxRoboticViaLocationOperationEx(this.macroDisplayer.ViaLocation).RefreshIcon();
  }

  private void SetProjectionDisplayerData()
  {
    if (this.projectionDisplayer == null)
      return;
    this.projectionDisplayer.CreationData = this.macroDisplayer.DisplayShape;
  }

  private void SetProjectionDisplayer()
  {
    if (this.projectionDisplayer != null)
    {
      this.projectionDisplayer.Delete();
      this.projectionDisplayer = (CApCMContinuousMacroProjectionDisplayer) null;
    }
    if (!this.isPreviewVisible)
      this.ProjectionWarning = ProjectionWarning.None;
    else if (this.Part == null)
    {
      this.ProjectionWarning = ProjectionWarning.NoPart;
    }
    else
    {
      this.projectionDisplayer = new CApCMContinuousMacroProjectionDisplayer(this.ViaLocation, this.displayParent, this.macroDisplayer.DisplayShape, this.Part);
      Action action = (Action) (() => this.ProjectionWarning = this.projectionDisplayer.IsNonEmpty ? ProjectionWarning.None : ProjectionWarning.NoProjection);
      this.projectionDisplayer.IsNonEmptyChanged += action;
      action();
    }
  }

  private void FireSendFeedback()
  {
    if (this.SendFeedback == null)
      return;
    this.SendFeedback();
  }
}
